package com.lanxiaoye.shoppingSystem.web;

import com.lanxiaoye.shoppingSystem.common.Constants;
import com.lanxiaoye.shoppingSystem.common.result.RequestResult;
import com.lanxiaoye.shoppingSystem.entity.*;
import com.lanxiaoye.shoppingSystem.service.ModuleService;
import com.lanxiaoye.shoppingSystem.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * Created by Administrator on 2017/3/22.
 */
@Controller
@RequestMapping("/module")
public class ModuleController extends BaseController{

    @Autowired
    ModuleService moduleService;

    @Autowired
    UserService userService;

    @RequestMapping("/suggest")
    @ResponseBody
    public RequestResult suggest(HttpServletRequest request){
        RequestResult requestResult = new RequestResult();
        String name = request.getParameter("userName");
        String phone = request.getParameter("userPhone");
        String email = request.getParameter("userEmail");
        String content = request.getParameter("content");
        Suggestion suggestion = new Suggestion();
        suggestion.setUserName(name);
        suggestion.setUserPhone(phone);
        suggestion.setUserEmail(email);
        suggestion.setSuggestion(content);
        int result = moduleService.addSuggestion(suggestion);
        if(result == 1){
            requestResult.setStatus(Constants.SUGGEST_SUCCEED);
            return requestResult;
        }
        requestResult.setStatus(Constants.SUGGEST_FAILED);
        return requestResult;
    }

    @RequestMapping("/infoModify")
    public String infoModify(HttpServletRequest request,Model model){
        String phone = request.getParameter("userPhone");
        String name = request.getParameter("userName");
        String email = request.getParameter("userEmail");
        String sex = request.getParameter("sex");
        String address = request.getParameter("address");
        String password = request.getParameter("password");
        sex = sex.equals("male")?"男":"女";
        User user1 = new User();
        user1.setPhone(phone);
        user1.setName(name);
        user1.setEmail(email);
        user1.setSex(sex);
        user1.setAddress(address);
        user1.setPassword(password);
        userService.update(user1);
        User user = userService.getUserByPhone(phone);
        model.addAttribute("User",user);
        return "/home/info";
    }

    @RequestMapping("/deleteAddress")
    @ResponseBody
    public RequestResult deleteAddress(HttpServletRequest request){
        RequestResult requestResult = new RequestResult();
        String id = request.getParameter("id");
        int result = moduleService.deleteReceiptInfo(Integer.parseInt(id));
        if(result == 1){
            requestResult.setStatus(Constants.DELETE_ADDRESS_SUCCEED);
            return requestResult;
        }
        requestResult.setStatus(Constants.DELETE_ADDRESS_FAILED);
        return requestResult;
    }

    @RequestMapping("/addAddress")
    @ResponseBody
    public RequestResult addAddress(HttpServletRequest request){
        RequestResult requestResult = new RequestResult();
        String userId = request.getParameter("userId");
        String name = request.getParameter("name");
        String phone = request.getParameter("phone");
        String address = request.getParameter("address");
        String code = request.getParameter("code");
        ReceiptInfo receiptInfo = new ReceiptInfo();
        receiptInfo.setUserId(Integer.parseInt(userId));
        receiptInfo.setName(name);
        receiptInfo.setPhone(phone);
        receiptInfo.setAddress(address);
        receiptInfo.setZipCode(code);
        int result = moduleService.addReceiptInfo(receiptInfo);
        if(result == 1){
            requestResult.setStatus(Constants.ADD_ADDRESS_SUCCEED);
            return requestResult;
        }
        requestResult.setStatus(Constants.ADD_ADDRESS_FAILED);
        return requestResult;
    }

    @RequestMapping("/removeCart")
    @ResponseBody
    public RequestResult removeCart(HttpServletRequest request){
        RequestResult requestResult = new RequestResult();
        String id = request.getParameter("id");
        int result = moduleService.deleteShoppingCart(Integer.parseInt(id));
        if(result == 1){
            requestResult.setStatus(Constants.REMOVE_CART_SUCCEED);
            return requestResult;
        }
        requestResult.setStatus(Constants.REMOVE_CART_FAILED);
        return requestResult;
    }

    @RequestMapping("/updateCart")
    @ResponseBody
    public RequestResult updateCart(HttpServletRequest request){
        RequestResult requestResult = new RequestResult();
        String id = request.getParameter("id");
        String goodsNum = request.getParameter("goodsNum");
        String totalFee = request.getParameter("totalFee");
        ShoppingCar shoppingCar = new ShoppingCar();
        shoppingCar.setId(Integer.parseInt(id));
        shoppingCar.setGoodsNum(Integer.parseInt(goodsNum));
        shoppingCar.setTotalFee(Float.parseFloat(totalFee));
        int result = moduleService.updateShoppingCart(shoppingCar);
        if(result == 1){
            requestResult.setStatus(Constants.UPDATE_CART_SUCCEED);
            return requestResult;
        }
        requestResult.setStatus(Constants.UPDATE_CART_FAILED);
        return requestResult;
    }

    @RequestMapping("/addOrder")
    @ResponseBody
    public RequestResult addOrder(HttpServletRequest request){
        RequestResult requestResult = new RequestResult();
        String userId = request.getParameter("userId");
        String goodsId = request.getParameter("goodsId");
        String goodsNum = request.getParameter("goodsNum");
        String deliveryFee = request.getParameter("deliveryFee");
        String finalTotalFee = request.getParameter("finalTotalFee");
        String receiptId = request.getParameter("receiptId");
        String status = request.getParameter("status");
        String message = request.getParameter("message");
        String payMethod = request.getParameter("payMethod");
        UserOrder userOrder = new UserOrder();
        userOrder.setUserId(Integer.parseInt(userId));
        userOrder.setGoodsId(Integer.parseInt(goodsId));
        userOrder.setGoodsNum(Integer.parseInt(goodsNum));
        userOrder.setDeliveryFee(Float.parseFloat(deliveryFee));
        userOrder.setFinalTotalFee(Float.parseFloat(finalTotalFee));
        userOrder.setReceiptId(Integer.parseInt(receiptId));
        userOrder.setStatus(status);
        userOrder.setMessage(message);
        userOrder.setPayMethod(payMethod);
        userOrder.setCreateTime(String.valueOf(new Date().getTime()));
        int result = moduleService.addOrder(userOrder);
        if(result == 1){
            requestResult.setStatus(Constants.ADD_ORDER_SUCCEED);
        }else{
            requestResult.setStatus(Constants.ADD_ORDER_FAILED);
        }
        return requestResult;
    }

    @RequestMapping("/cancelOrder")
    @ResponseBody
    public RequestResult cancelOrder(HttpServletRequest request){
        RequestResult requestResult = new RequestResult();
        String userId = request.getParameter("userId");
        int result = moduleService.cancelOrder(Integer.parseInt(userId));
        if(result == 0){
            requestResult.setStatus(Constants.CANCEL_ORDER_FAILED);
        }else{
            requestResult.setStatus(Constants.CANCEL_ORDER_SUCCEED);
        }
        return requestResult;
    }

    @RequestMapping("/payOrder")
    @ResponseBody
    public RequestResult payOrder(HttpServletRequest request){
        RequestResult requestResult = new RequestResult();
        String userId = request.getParameter("userId");
        int result = moduleService.payOrder(Integer.parseInt(userId));
        if(result == 0){
            requestResult.setStatus(Constants.PAY_ORDER_FAILED);
        }else{
            requestResult.setStatus(Constants.PAY_ORDER_SUCCEED);
        }
        return requestResult;
    }

    @RequestMapping("/clearCart")
    @ResponseBody
    public RequestResult clearCart(HttpServletRequest request){
        RequestResult requestResult = new RequestResult();
        String userId = request.getParameter("userId");
        int result = moduleService.deleteCartByUserId(Integer.parseInt(userId));
        if(result == 0){
            requestResult.setStatus(Constants.CLEAR_CART_FAILED);
        }else{
            requestResult.setStatus(Constants.CLEAR_CART_SUCCEED);
        }
        return requestResult;
    }

    @RequestMapping("/comment")
    @ResponseBody
    public RequestResult comment(HttpServletRequest request){
        RequestResult requestResult = new RequestResult();
        String star = request.getParameter("star");
        String content = request.getParameter("content");
        String orderId = request.getParameter("orderId");
        UserOrder order = moduleService.getOrderByOrderId(Integer.parseInt(orderId));
        Comment comment1 = moduleService.getCommentByOrderId(order.getOrderId());
        if(comment1 != null){
            requestResult.setStatus(Constants.HAD_COMMENT);
            return requestResult;
        }
        int goodsId = order.getGoodsId();
        User user = userService.getUserById(order.getUserId());
        Comment comment = new Comment();
        comment.setGoodsId(goodsId);
        comment.setUserName(user.getName());
        comment.setOrderId(order.getOrderId());
        comment.setStar(Integer.parseInt(star));
        comment.setContent(content);
        comment.setCreateTime(String.valueOf(new Date().getTime()));
        int result = moduleService.addComment(comment);
        if(result == 1){
            requestResult.setStatus(Constants.COMMENT_SUCCEED);
        }else{
            requestResult.setStatus(Constants.COMMENT_FAILED);
        }
        return requestResult;
    }
}
